package com.pkk.penguin.frame.know.java.algorithm;

import cn.hutool.core.util.RandomUtil;
import java.util.Arrays;
import java.util.HashSet;
import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * @description: 一个数组的第K大值
 * @author: peikunkun
 * @create: 2020-05-07 14:08
 **/
@Slf4j
public class ArrayMaxK {

  public final static int ILEN = 10;
  public static Integer[] arrs = new Integer[ILEN];


  /**
   * 第几大值
   */
  public static int K = RandomUtil.randomInt(0, ILEN);


  static {
    for (int i = 0; i < ILEN; i++) {
      arrs[i] = RandomUtil.randomNumber();
    }
  }


  @Before
  public void before() {
    log.info("生成的随即数组为:{}", arrs);
  }


  @Test
  public void Administrator_21_20200507140917() {
    System.out.println("欢迎使用单元测试方法【Administrator_21()_20200507140917】");
    System.out.println("此方法测试描述：【利用数组的排序,进行不分组的最大值K值得配置】");
    Integer[] arrs1 = arrs.clone();
    log.info("元数据:{}", arrs1);
    Arrays.sort(arrs1);
    log.info("现数据:{}", arrs1);
    log.info("第{}大的数据值:{}", K, (arrs1[ILEN - K]));
  }


  @Test
  public void Administrator_54_20200507144426() {
    System.out.println("欢迎使用单元测试方法【Administrator_54()_20200507144426】");
    System.out.println("此方法测试描述：【去重之后的第K大值】");

    Integer[] arrnew = arrs.clone();
    final HashSet<Integer> set = new HashSet<Integer>(Arrays.asList(arrnew));
    final Integer[] integers = set.toArray(new Integer[]{});
    Arrays.sort(integers);
    K = 2;
    log.info("获取的info信息为:{}", Arrays.toString(set.toArray()));
    log.info("第{}大的数据值:{}", K, (integers[integers.length - K]));
  }


  @After
  public void after() {
    log.info("生成的随即数组为:{}", arrs);
  }

}
